<template>
  <div>
    <el-form inline :model="queryData">
      <el-form-item label="标题" >
        <el-input v-model="queryData.title" placeholder="请输入标题"/>
      </el-form-item>
      <el-form-item label="类型">
        <el-select v-model="queryData.type"  placeholder="请选择类型" clearable @clear="formClear('type')">
          <el-option :label="item" :value="index + 1" v-for="(item,index) in typeArr" :key="index"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查 询</el-button>
        <el-button type="warning" @click="entityInfoEvent(null)">新 增</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableList" border style="width: 100%;">
      <el-table-column prop="image" label="图片" align="center" width="380">
        <template slot-scope="{row}">
          <el-image class="element-image" fit="cover" :src="row.image"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="title" label="标题" align="center"/>
      <el-table-column prop="type" label="类型" :formatter="(row) => typeArr[row.type - 1]"
                       align="center"></el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180" align="center"/>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="{row}">
          <el-button type="text" @click="entityInfoEvent(row)">编辑</el-button>
          <el-button type="text" @click="deleteEvent(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Paging v-model="queryData" @change="getRequest"></Paging>


    <el-drawer title="轮播图信息" :visible.sync="drawer" direction="rtl" :before-close="drawerClose" size="500px"
               destroy-on-close :wrapperClosable="false" class="drawer-css">
      <SwiperForm :entity="entity" v-if="drawer" @close="drawerClose" @refresh="getRequest"></SwiperForm>
    </el-drawer>

  </div>
</template>

<script>
  import {mapGetters} from 'vuex'
  export default {
    name: 'list',
    components: {
      'SwiperForm': () => import('./SwiperForm')
    },
    data() {
      return {
        typeArr: ['企业', '园区', '电商联运', '银企直连', '政府招商', '营销工具'],
        tableList: [],
        queryData: {
          title: null, // 标题
          type: null, // 类型：1企业；2园区
          pageNum: 1,
          pageSize: 10,
          total: 0
        },
        formData: {},
        entity: null,
        drawer: false

      }
    },
    beforeMount() {
      this.getRequest()
    },
    computed: {
      ...mapGetters(['confirm']),
    },
    methods: {
      /**
       * form置空事件
       */
      formClear(key) {
        this.queryData[key] = null
        this.onSubmit()
      },
      /**
       * 查询条件提交事件
       */
      onSubmit() {
        this.queryData.pageNum = 1;
        this.getRequest()
      },
      /**
       * 列表请求事件
       */
      async getRequest(event) {
        const res = await this.$ajax.get('/wx/rotation/rotation/list', { params: this.queryData })
        this.queryData.total = res.total
        this.tableList = res.rows
      },
      /**
       * 新增 或 修改事件
       * row null 新增
       */
      entityInfoEvent(row) {
        this.entity = row;
        this.drawer = true;
      },
      /**
       * 删除事件
       */
      deleteEvent (row) {
        this.$confirm(this.confirm.text, '提示', this.confirm.options).then(async () => {
          await this.$ajax.get(`/wx/rotation/id?id=${row.id}`)
          this.$message.success('操作成功')
          this.onSubmit()
        })
      },
      /**
       * 取消抽屉事件
       */
      drawerClose() {
        this.drawer = false
      }
    }
  }
</script>

<style scoped lang="scss">
  .table-css {
    width: 100%;
  }

  .element-image {
    width: 356px;
    height: 114px;
  }
</style>
